﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Community.CsharpSqlite.Entity
{

    /*
** State information local to the memory allocation subsystem.
*/
    //static SQLITE_WSD struct Mem0Global {
    public class Mem0Global
    {
        /* Number of free pages for scratch and page-cache memory */

        /*
  ** Pointers to the end of Global.Config.pScratch and
  ** Global.Config.pPage to a block of memory that records
  ** which pages are available.
  */
        public BtCursor[] aBtCursor;
        public byte[][][] aByte;
        public int[] aByteSize;
        public int[] aByte_used;
        public int[][] aInt;
        public Mem[] aMem;
        public object alarmArg;
        public dxalarmCallback alarmCallback; // (*alarmCallback)(void*, long,int);
        public long alarmThreshold;
        public memstat msBtCursor;

        public memstat msByte;
        public memstat msInt;
        public memstat msMem;
        public sqlite3_mutex mutex; /* Mutex to serialize access */
        public int nPageFree;
        public int nScratchFree;

        public Mem0Global()
        {
        }

        public Mem0Global(int nScratchFree, int nPageFree, sqlite3_mutex mutex, long alarmThreshold,
                          dxalarmCallback alarmCallback, object alarmArg, int Byte_Allocation, int Int_Allocation,
                          int Mem_Allocation, int BtCursor_Allocation)
        {
            this.nScratchFree = nScratchFree;
            this.nPageFree = nPageFree;
            this.mutex = mutex;
            this.alarmThreshold = alarmThreshold;
            this.alarmCallback = alarmCallback;
            this.alarmArg = alarmArg;
            msByte.next = -1;
            msInt.next = -1;
            msMem.next = -1;
            aByteSize = new[] { 32, 256, 1024, 8192, 0 };
            aByte_used = new[] { -1, -1, -1, -1, -1 };
            aByte = new byte[aByteSize.Length][][];
            for (int i = 0; i < aByteSize.Length; i++) aByte[i] = new byte[Byte_Allocation][];
            aInt = new int[Int_Allocation][];
            aMem = new Mem[Mem_Allocation <= 4 ? 4 : Mem_Allocation];
            aBtCursor = new BtCursor[BtCursor_Allocation <= 4 ? 4 : BtCursor_Allocation];
        }

        #region Nested type: memstat

        public struct memstat
        {
            public int alloc; // # of allocation requests
            public int cached; // # of cache hits
            public int dealloc; // # of deallocations
            public int max; // # Max slot used
            public int next; // # Next slot to use
        }

        #endregion
    }
}
